package com.xuduo.amd.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.text.AbstractDocument;
import java.security.SecureRandom;
import java.util.Base64;

public class AESUtils {
    private static final Logger logger= LoggerFactory.getLogger(AESUtils.class);

    private static  final String KEY_ALGORITHM="AES";
    private static  final String DEFAULT_CIPHER_ALGORITHM="AES/ECB/PKCS5Padding";//默认的加密算法


    /**
     * AES加密操作
     * @param context 明文
     * @param password 加密密码
     * @return  Base64转码后的密文
     */
    public static String encrypt(String content,String password){
        try {
            Cipher cipher=Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);//创建密码器
            cipher.init(Cipher.ENCRYPT_MODE,getSecretKey(password));//初始化为加密模式密码器
            byte[] byteContent= content.getBytes("utf-8");
            byte[] result = cipher.doFinal(byteContent);
            return Base64.encodeBase64String(result);


        }catch (Exception ex){
            logger.error("加密失败:{}",ex.getMessage());
        }
        return null;
    }

    /**
     * AES解密操作
     * @param content  密文
     * @param password  解密密码
     * @return 明文
     */
    public static String decrypy(String content,String password){
        try {
            Cipher cipher=Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);//实例化
            cipher.init(Cipher.ENCRYPT_MODE,getSecretKey(password));//使用密钥初始化，设置为解密模式
            byte[] bytes= Base64.decodeBase64(content);
            byte[] result = cipher.doFinal(bytes);
            return new String(result,"utf-8");


        }catch (Exception ex){
            logger.error("解密失败:{}",ex.getMessage());
        }
        return null;
    }


    public static String decrypy(byte[] key,String content){
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key,KEY_ALGORITHM);
            Cipher cipher=Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);//实例化
            cipher.init(Cipher.ENCRYPT_MODE,keySpec);//使用密钥初始化，设置为解密模式
            byte[] bytes= Base64.decodeBase64(content);
            byte[] result = cipher.doFinal(bytes);
            return new String(result,"utf-8");

        }catch (Exception ex){
            logger.error("解密失败:{}",ex.getMessage());
        }
        return null;
    }



    public static String getSecretKey(final String password){
        KeyGenerator kg=null;
        try {
            SecureRandom random = SecureRandom.getInstance("SHAlPRNG");
            random.setSeed(password.getBytes());
            kg=KeyGenerator.getInstance(KEY_ALGORITHM);
            kg.init(128,random);
            //生成密钥
            SecretKey secretKey=kg.generateKey();
            byte[] enCodeFormat=secretKey.getEncoded();

            return new SecretKeySpec(enCodeFormat,KEY_ALGORITHM);

        }catch (Exception ex){
            logger.error("生成密钥失败:{}",ex.getMessage());
        }
        return null;
    }










}
